package com.lwb.ztx.lives.widget;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.lwb.ztx.R;
import com.lwb.ztx.bean.LiveBean;
import com.lwb.ztx.utils.ListUtils;

import java.util.List;

/**
 * Created by luweibin on 2016/6/12.
 */
public class LiveChannelAdapter extends RecyclerView.Adapter {
    OnChannelItemClickListener listener;

    private List<LiveBean> liveBeanList;

    public LiveChannelAdapter(OnChannelItemClickListener listener,List<LiveBean> liveBeanList) {
        this.listener = listener;
        this.liveBeanList = liveBeanList;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.live_channel_item, parent, false);
        return new LiveChannelViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        LiveChannelViewHolder liveChannelViewHolder = (LiveChannelViewHolder) holder;
        liveChannelViewHolder.liveBean = liveBeanList.get(position);
        liveChannelViewHolder.title.setText(liveBeanList.get(position).getTitle());
        if (liveBeanList.get(position).getRes() != 0) {
            liveChannelViewHolder.imageView.setImageResource(liveBeanList.get(position).getRes());
        } else {
            liveChannelViewHolder.imageView.setImageResource(R.drawable.tv_default);
        }
    }

    @Override
    public int getItemCount() {
        return ListUtils.getSize(liveBeanList);
    }

    class LiveChannelViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        LiveBean liveBean;
        TextView title;
        ImageView imageView;
        View bgView;
        public LiveChannelViewHolder(View itemView) {
            super(itemView);
            itemView.setOnClickListener(this);
            title = (TextView) itemView.findViewById(R.id.live_title);
            imageView = (ImageView) itemView.findViewById(R.id.live_image);
            bgView = itemView.findViewById(R.id.live_item_click_bg_view);
            bgView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (listener != null) {
                listener.onClickItem(liveBean);
            }
        }
    }

    public interface OnChannelItemClickListener {
        void onClickItem(LiveBean bean);
    }

    public void addLives(List<LiveBean> beans) {
        this.liveBeanList.addAll(beans);
    }

    public List<LiveBean> getLiveBeanList() {
        return liveBeanList;
    }
}
